<?php
namespace Shoptaemob\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $user;
	public function  __construct (){
		parent::__construct();
		$this->assign("appkey",C("FT_KEY"));
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$sid = I("sid");
		if($sid){
			cookie("sid",$sid);
			$this->sid = $sid;
			//cookie保存sid，读取不到则跳出
		}else{
			$this->sid = cookie("sid");
		}
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Shopmobile@Index:not_allow");
			die();
		}
		$this->mixednick = I("tida_mixnick");
		if($this->mixednick){
			$this->friend = I("friend");
			// $this->user = S($this->mixednick);
			if(!$this->user){
				$params = Array(
							"m"=>"shopmobile",
							"c"=>"index",
							"a"=>"ajax_user",
							"sid"=>$sid,
							"tida_mixnick"=>$this->mixednick,
							"friend"=>$this->friend,
						  );
				$this->user = json_decode($this->http(C("TAE_URL"),$params),true);
				S($this->mixednick,$this->user);
			}
		}
		$this->sys_uid = $this->user["sys_uid"];
		$this->shop_uid = $this->user["shop_uid"];
		$this->assign("appname",cookie("appname"));
		$this->assign("codeimg",$codeimg);
		$this->assign("sid",$this->sid);
		$this->assign("user",$this->user);
	}
	
	/**
	 * 发送HTTP请求方法
	 * @param  string $url    请求URL
	 * @param  array  $params 请求参数
	 * @param  string $method 请求方法GET/POST
	 * @return array  $data   响应数据
	 */
	function http($url, $params, $method = 'GET', $header = array(), $multi = false){
		$opts = array(
				CURLOPT_TIMEOUT        => 30,
				CURLOPT_RETURNTRANSFER => 1,
				CURLOPT_SSL_VERIFYPEER => false,
				CURLOPT_SSL_VERIFYHOST => false,
				CURLOPT_HTTPHEADER     => $header
		);
		/* 根据请求类型设置特定参数 */
		switch(strtoupper($method)){
			case 'GET':
				$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
				break;
			case 'POST':
				//判断是否传输文件
				$params = $multi ? $params : http_build_query($params);
				$opts[CURLOPT_URL] = $url;
				$opts[CURLOPT_POST] = 1;
				$opts[CURLOPT_POSTFIELDS] = $params;
				break;
			default:
				throw new \Exception('不支持的请求方式！');
		}
		/* 初始化并执行curl请求 */
		$ch = curl_init();
		curl_setopt_array($ch, $opts);
		$data  = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);
		if($error) throw new \Exception('请求发生错误：' . $error);
		return  $data;
	}

}